package cn.iutils.edu.controller;

import cn.iutils.edu.entity.TypeConfig;
import cn.iutils.edu.service.TypeConfigService;
import cn.iutils.sys.entity.Organization;
import cn.iutils.sys.service.OrganizationService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import cn.iutils.common.Page;
import cn.iutils.common.utils.JStringUtils;
import cn.iutils.common.BaseController;
import cn.iutils.edu.entity.Asset;
import cn.iutils.edu.service.AssetService;

import java.util.ArrayList;
import java.util.List;

/**
* 学校资产 控制器
* @author iutils.cn
* @version 1.0
*/
@Controller
@RequestMapping("${adminPath}/asset")
public class AssetController extends BaseController {

    @Autowired
    private AssetService assetService;
    @Autowired
    private OrganizationService organizationService;
    @Autowired
    private TypeConfigService typeConfigService;

    @ModelAttribute
    public Asset get(@RequestParam(required = false) String id) {
        Asset entity = null;
        if (JStringUtils.isNotBlank(id)) {
            entity = assetService.get(id);
        }
        if (entity == null) {
            entity = new Asset();
        }
        return entity;
    }

    @RequiresPermissions("edu:asset:view")
    @RequestMapping(value = "/list")
    public String list(Model model, Page<Asset> page) {
        model.addAttribute("page", page.setList(assetService.findPage(page)));
        return "edu/asset/list";
    }

    @RequiresPermissions("edu:asset:create")
    @RequestMapping(value = "/create", method = RequestMethod.GET)
    public String create(Asset asset,Model model,String moduleName) {
        toModule(asset, model);
        return "edu/asset/form";
    }

    @RequiresPermissions("edu:asset:create")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public String create(Asset asset, RedirectAttributes redirectAttributes) {
        assetService.save(asset);
        addMessage(redirectAttributes,"新增成功");
        return "redirect:"+ adminPath +"/asset/update?id="+asset.getId();
    }

    @RequiresPermissions("edu:asset:update")
    @RequestMapping(value = "/update", method = RequestMethod.GET)
    public String update(Asset asset, Model model) {
        toModule(asset, model);
        return "edu/asset/form";
    }

    /**
     * 抽取相同的方法
     * @param asset
     * @param model
     */
    private void toModule(Asset asset, Model model) {
        Organization organization = organizationService.get(new Organization());
        List<TypeConfig> typeList = new ArrayList<TypeConfig>();
        typeList = typeConfigService.getModileTypeList("assetType");
        model.addAttribute("asset", asset);
        model.addAttribute("organization",organization);
        model.addAttribute("assetType",typeList);
    }

    @RequiresPermissions("edu:asset:update")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String update(Asset asset, RedirectAttributes redirectAttributes) {
        assetService.save(asset);
        addMessage(redirectAttributes,"修改成功");
        return "redirect:"+ adminPath +"/asset/update?id="+asset.getId();
    }

    @RequiresPermissions("edu:asset:delete")
    @RequestMapping(value = "/{id}/delete", method = RequestMethod.GET)
    public String delete(@PathVariable("id") String id,int pageNo,int pageSize, RedirectAttributes redirectAttributes) {
        assetService.delete(id);
        addMessage(redirectAttributes,"删除成功");
        return "redirect:"+ adminPath +"/asset/list?pageNo="+pageNo+"&pageSize="+pageSize;
    }
}
